﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Happy.Web.Mvc;
using Happy.Query;
using Happy.Cms.Query.Categories;
using Happy.Cms.Query.Articles;
using Happy.Cms.Application.Categories;
using Happy.Cms.Application.Articles;

namespace Happy.Cms.Controllers.Utils
{
    public static class HappyControllerExtentions
    {
        public static IEnumerable<ArticleView> FindArticlesByCategoryName(this HappyController that, string categoryName)
        {
            var categoryQueryService = that.Service<CategoryQueryService>();
            var articleQueryService = that.Service<ArticleQueryService>();

            var category = categoryQueryService.FindByName(categoryName);
            return articleQueryService.FindByCategoryId(category.Id);
        }

        public static IEnumerable<ArticleView> FindArticlesByCategoryName(this HappyController that, string categoryName, int top)
        {
            var categoryQueryService = that.Service<CategoryQueryService>();
            var articleQueryService = that.Service<ArticleQueryService>();

            var category = categoryQueryService.FindByName(categoryName);
            return articleQueryService.FindByCategoryId(category.Id, top);
        }

        public static PageResult<ArticleView> FindArticlesByCategoryName(this HappyController that, string categoryName, long page, long itemsPerPage)
        {
            var categoryQueryService = that.Service<CategoryQueryService>();
            var articleQueryService = that.Service<ArticleQueryService>();

            var category = categoryQueryService.FindByName(categoryName);
            return articleQueryService.FindByCategoryId(category.Id, page, itemsPerPage);
        }
    }
}
